package api;

import cn.hutool.json.JSONUtil;
import common.ApplicationVariable;
import lombok.SneakyThrows;
import model.ResultData;
import util.DBUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * @description: 修改订单状态
 * @author: chengbaiyi
 * @create: 2022-06-27 11:06
 */
@WebServlet("/uporder")
public class UpdateOrderServlet extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int state = -1;
        String msg = "未知错误！";
        int data = -1;
        int oid = Integer.parseInt(req.getParameter("oid"));
        int s = Integer.parseInt(req.getParameter("state"));
        if (oid > 0 && s >= 0) {
            Connection connection = DBUtil.getConnection();
            String sql = "update `order` set state=? where oid=?";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setInt(1, s);
            statement.setInt(2, oid);
            int result = statement.executeUpdate();
            if (result > 0) {
                state = 200;
                data = 1;
                msg = "操作成功！";
            }
            DBUtil.close(null, statement, connection);
        } else {
            msg = "参数错误";
        }
        resp.setContentType(ApplicationVariable.RESPONSE_CONTENT_TYPE);
        resp.getWriter().println(
                JSONUtil.toJsonStr(new ResultData(state, data, msg))
        );
    }
}
